local addonName, IWonew = ...

function SendCraftMailItem666()
  local realmName = GetRealmName()
  local roleName = "@dddx小拉"
  if realmName == "死亡之翼" then
    roleName = "小兔丶罗西"
  end
  if realmName == "白银之手" then
    roleName = "小狼温蒂丶"
  end
  -- /dump GetRealmName()
  -- 223692 晶脉辉煌3星
  -- 222428 镔爪合金3星
  -- 223790 诅咒急速3星
  local itemIDs = {222428, 223692, 223790}
  for i = 1, #itemIDs do
    local itemSlot = IWonew.GetItemSlot(itemIDs[i])
    if itemSlot then
      print("itemSlot", itemSlot.bagID, itemSlot.slotID)
      C_Container.PickupContainerItem(itemSlot.bagID, itemSlot.slotID)
      ClickSendMailItemButton(i)
    end
  end
  SendMail(roleName, "2", "")
end

function SendItemToRole(roleName, itemSlotList, callback)
  if #itemSlotList == 0 then
    print("没有找到物品")
    return
   end
  local i = 1
  local ticker
  ticker = C_Timer.NewTicker(0.3, function()
    print("i--", i, "ss---", itemSlotList[i].bagID, itemSlotList[i].slotID)
    C_Container.PickupContainerItem(itemSlotList[i].bagID, itemSlotList[i].slotID)
    ClickSendMailItemButton(i)
    i = i + 1
    if i > #itemSlotList then
      ticker:Cancel()
      SendMail(roleName, "2", "")
      print("send to: ", roleName)
      C_Timer.After(1, function()
        if callback then
          callback()
        end
      end)
    end
  end)
  -- for i = 1, #itemSlotList do
  --   print("iii---", i, "bbb", itemSlotList[i].bagID, itemSlotList[i].slotID)
    
  --   -- local info = C_Container.GetContainerItemInfo(itemSlotList[i].bagID, itemSlotList[i].slotID)
    
  --   print("send to: ", roleName)
    
  -- end
end

local SendSplitItemRoleIdx = 0
local SendSplitItemDone = false
local AutoSendItemToRoleList_ticker = nil
-- /dump AutoSendItemToRoleList(1, 20, 213610, 6)
function AutoSendItemToRoleList(page, pageSize, itemID, stack)
  SendSplitItemRoleIdx = 0
  SendSplitItemDone = false
  if AutoSendItemToRoleList_ticker then
    AutoSendItemToRoleList_ticker:Cancel()
    AutoSendItemToRoleList_ticker = nil
    print("停止自动发送！！")
    return
  end
  AutoSendItemToRoleList_ticker = C_Timer.NewTicker(1, function()
    if SendSplitItemDone then
      print("Send Done!!")
      if AutoSendItemToRoleList_ticker then
        AutoSendItemToRoleList_ticker:Cancel()
        AutoSendItemToRoleList_ticker = nil
      end
    else
      SendCraftReagentsToRole(page, pageSize, itemID, stack)
    end
  end)
end


-- 拆分发送物品
-- https://www.curseforge.com/wow/addons/reenhancedstacksplit/download/5650893
-- /dump SendCraftReagentsToRole(213610, 6)
function SendCraftReagentsToRole(page, pageSize, itemID, stack)
  if SendSplitItemDone then return end
  local roleList = {
    -- {name = "强丶壮", realm = "死亡之翼"},
    -- {name = "李乄白", realm = "死亡之翼"},
    -- {name = "小狼温蒂丶", realm = "白银之手"},
    -- {name = "小兔丶罗西", realm = "死亡之翼"},
    -- {name = "小老虎丶泰格", realm = "白银之手"},
    -- {name = "小狮子丶里奥", realm = "白银之手"},
    -- {name = "小熊猫丶佩吉", realm = "白银之手"},
    -- {name = "小毛驴丶迪安", realm = "白银之手"},
    -- {name = "狐狸弗雷德丶", realm = "白银之手"},
    -- {name = "坎丶龙人", realm = "白银之手"},
    -- {name = "坤丶乔治", realm = "白银之手"},
    -- {name = "斑马苏怡丶", realm = "白银之手"},
    -- {name = "坤丶佩奇", realm = "白银之手"},
    -- {name = "袋鼠丶乔伊", realm = "白银之手"},
    -- {name = "泽塔丶", realm = "死亡之翼"},
    -- {name = "小松鼠丶西蒙", realm = "白银之手"},
    -- {name = "斑马丶祖祖", realm = "白银之手"},
    -- {name = "小象丶艾德蒙", realm = "死亡之翼"},
    -- {name = "小象丶艾米丽", realm = "死亡之翼"},
    -- {name = "斑马丶莎莎", realm = "死亡之翼"},
    -- {name = "小兔丶罗比", realm = "死亡之翼"},

    -- {name = "小狗丹尼丶", realm = "白银之手"},
    -- {name = "小熊贝琳达丶", realm = "白银之手"},
    -- {name = "袋鼠凯莉丶", realm = "白银之手"},
    -- {name = "小兔理查德丶", realm = "白银之手"},
    -- {name = "小毛驴戴芬丶", realm = "白银之手"},
    -- {name = "小黑萨", realm = "白银之手"},
    -- {name = "艾斯比丶", realm = "白银之手"},
    -- {name = "工神黄月英", realm = "白银之手"},
    -- {name = "强壮的如花", realm = "白银之手"},
    -- {name = "徐元植", realm = "白银之手"},

    {name = "火星野人", realm = "白银之手"},
    {name = "小马佩德罗丶", realm = "白银之手"},
    {name = "很丶强", realm = "白银之手"},
    {name = "壮士请灭灯", realm = "白银之手"},
    {name = "联盟酋長", realm = "白银之手"},
    {name = "机械狗", realm = "白银之手"},
    {name = "王立宏", realm = "白银之手"},
    {name = "奥丁尼尔", realm = "白银之手"},
    {name = "希瓦的守護", realm = "白银之手"},

    -- {name = "机械火鸡", realm = "白银之手"},
    -- {name = "壮汉诸葛恪", realm = "白银之手"},
    -- {name = "小羊苏西丶", realm = "死亡之翼"},
    -- {name = "机械法丝", realm = "白银之手"},
  }
  local realmRoleList = {}
  local curRealmName = GetRealmName()
  for i = 1, #roleList do
    if roleList[i].realm == curRealmName then
      table.insert(realmRoleList, roleList[i].name)
    end
  end
  local pageRoleList = IWonew.slice(realmRoleList, (page - 1) * pageSize + 1, page * pageSize)
  SendSplitItemRoleIdx = SendSplitItemRoleIdx + 1
  if SendSplitItemRoleIdx > #pageRoleList then
    SendSplitItemDone = true
    return
  end
  local itemSlot = IWonew.FilterItemSlotByIDAndQuality(itemID, stack)
  -- local itemSlot_QSHJ_212306 = IWonew.FilterItemSlotByIDAndQuality(212306, 1)
  local curRole = pageRoleList[SendSplitItemRoleIdx]
  local mergedItems = {}
  if #itemSlot > 0 then
    table.insert(mergedItems, itemSlot[1])
  end
  -- if #itemSlot_QSHJ_212306 > 0 then
  --   table.insert(mergedItems, itemSlot_QSHJ_212306[1])
  -- end
  SendItemToRole(curRole, mergedItems)
end


-- /dump AutoSplitItemStack(213610, 6)
function AutoSplitItemStack(itemID, stack)
  local ticker
  local count = 1
  local itemSlot = IWonew.GetItemSlot(itemID)
  local freeSlots = IWonew.GetFreeBagSlots()
  ticker = C_Timer.NewTicker(1, function()
    if count > 20 then
      print("Split Done!!")
      ticker:Cancel()
    else
      C_Container.SplitContainerItem(itemSlot.bagID, itemSlot.slotID, stack)
      C_Container.PickupContainerItem(freeSlots[count].bagID, freeSlots[count].slotID)
      count = count + 1
    end
  end)
end

-- /dump AutoSend6CardToRole()

local AutoSend6CardToRoleIdx = 1
-- /dump AutoSend6CardToRole()
function AutoSend6CardToRole(itemID, startIdx, count)
  -- 盎溢之
  -- 共生之
  -- 扬升之
  -- 光辉之

  AutoSend6CardToRoleIdx = startIdx
  local selectAccountList = {}
  AccountRoleList = AccountRoleList or {}
  for i = startIdx, count do
    table.insert(selectAccountList, AccountRoleList[i])
  end

  local curRole = AccountRoleList[AutoSend6CardToRoleIdx]
  if curRole == nil then
    AutoSend6CardToRoleIdx = 1
    print("Send Done!!")
    return
  end
  print("curRole", curRole.name)
  local cardSlots = IWonew.FilterItemSlotByIDAndQuality(itemID or 222653, 1)
  SendItemToRole(curRole.name, IWonew.slice(cardSlots, 1, 6), function()
    AutoSend6CardToRoleIdx = AutoSend6CardToRoleIdx + 1
    AutoSend6CardToRole(itemID)
  end)
end
